/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.sofa.shark.admin.service.impl;


import cn.hutool.core.date.DateUtil;
import com.alipay.sofa.rpc.common.RpcConstants;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sofa.shark.admin.entity.SysFiles;
import com.sofa.shark.admin.mapper.SysFilesMapper;
import com.sofa.shark.admin.service.SysFilesServices;
import com.sofa.shark.common.core.constant.CommonConstants;
import com.sofa.shark.common.core.enums.ExceptionEnums;
import com.sofa.shark.common.core.exception.BusinessException;
import com.sofa.shark.file.entity.FileInfoBO;
import com.sofa.shark.file.entity.FileServerResponse;
import com.sofa.shark.file.enums.FileUploadSceneType;
import com.sofa.shark.file.remotefile.FileService;
import com.sofa.shark.file.uitls.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;

/**
 * 系统文件表
 *
 * @author tonypy
 * @date 2020-04-01 14:48:16
 */
@Service
@Slf4j
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFiles> implements SysFilesServices {

    @SofaReference(interfaceType = FileService.class, binding = @SofaReferenceBinding(bindingType = RpcConstants.PROTOCOL_TYPE_BOLT))
    private FileService fileService;

    @Override
    public SysFiles saveFiles(MultipartFile multipartFile, FileUploadSceneType sceneType) {
        String filePath = FileUtils.getClassPath();
        File parentFile = new File(filePath);
        if (parentFile.exists()) {
            if (!parentFile.isDirectory()) {
                throw new BusinessException(ExceptionEnums.IS_NOT_DIR);
            }
        } else {
            parentFile.mkdir();
        }
        File file = FileUtils.getFile(multipartFile, filePath);
        return saveFiles(file, sceneType);
    }

    private SysFiles saveFiles(File file, FileUploadSceneType sceneType) {
        SysFiles sysFiles = new SysFiles();
        Date date = DateUtil.date();
        Integer year = DateUtil.year(date);
        Integer month = DateUtil.month(date);
        //获取月数时间从0开始
        Integer day = DateUtil.dayOfMonth(date)+1;
        Integer hour = DateUtil.hour(date, true);
        String path = sceneType.name() + "/" + year + "/" + month + "/" + day + "/" + hour;
        FileServerResponse<FileInfoBO> upload = fileService.upload(file, path, sceneType);
        if (upload != null && CommonConstants.OK.equals(upload.getStatus())) {
            if (upload.getData() != null) {
                sysFiles.setPictureId(upload.getData().getMd5());
                sysFiles.setPath(upload.getData().getPath());
                sysFiles.setRelativePath(upload.getData().getPath());
                int index = upload.getData().getPath().lastIndexOf("/");
                int len = upload.getData().getPath().length();
                sysFiles.setName(upload.getData().getPath().substring(index + 1, len));
                sysFiles.setScene(sceneType.name());
                sysFiles.setSize(upload.getData().getSize());
                sysFiles.setDomain(upload.getData().getDomain());
                SysFiles newSysFiles = new SysFiles();
                newSysFiles.setPictureId(upload.getData().getMd5());
                SysFiles selectOne = this.baseMapper.selectOne(Wrappers.query(newSysFiles));
                if (selectOne != null) {
                    return selectOne;
                } else {
                    this.baseMapper.insert(sysFiles);
                }
            }
        } else {
            throw new BusinessException(ExceptionEnums.UPLOAD_FILE_ERROR);
        }
        return sysFiles;
    }
}
